package com.ruoyi.am.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.am.domain.AmDriving;
import com.ruoyi.am.service.IAmDrivingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 行车日志Controller
 * 
 * @author hkkk
 * @date 2020-11-29
 */
@Controller
@RequestMapping("/am/driving")
public class AmDrivingController extends BaseController
{
    private String prefix = "am/driving";

    @Autowired
    private IAmDrivingService amDrivingService;

    @RequiresPermissions("am:driving:view")
    @GetMapping()
    public String driving()
    {
        return prefix + "/driving";
    }

    /**
     * 查询行车日志列表
     */
    @RequiresPermissions("am:driving:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmDriving amDriving)
    {
        startPage();
        List<AmDriving> list = amDrivingService.selectAmDrivingList(amDriving);
        return getDataTable(list);
    }

    /**
     * 导出行车日志列表
     */
    @RequiresPermissions("am:driving:export")
    @Log(title = "行车日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmDriving amDriving)
    {
        List<AmDriving> list = amDrivingService.selectAmDrivingList(amDriving);
        ExcelUtil<AmDriving> util = new ExcelUtil<AmDriving>(AmDriving.class);
        return util.exportExcel(list, "driving");
    }

    /**
     * 新增行车日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存行车日志
     */
    @RequiresPermissions("am:driving:add")
    @Log(title = "行车日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmDriving amDriving)
    {
        return toAjax(amDrivingService.insertAmDriving(amDriving));
    }

    /**
     * 修改行车日志
     */
    @GetMapping("/edit/{drivingId}")
    public String edit(@PathVariable("drivingId") Long drivingId, ModelMap mmap)
    {
        AmDriving amDriving = amDrivingService.selectAmDrivingById(drivingId);
        mmap.put("amDriving", amDriving);
        return prefix + "/edit";
    }

    /**
     * 修改保存行车日志
     */
//    @RequiresPermissions("am:driving:edit")
    @Log(title = "行车日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmDriving amDriving)
    {
        return toAjax(amDrivingService.updateAmDriving(amDriving));
    }

    /**
     * 删除行车日志
     */
    @RequiresPermissions("am:driving:remove")
    @Log(title = "行车日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amDrivingService.deleteAmDrivingByIds(ids));
    }

    @PostMapping("/addDriving")
    @ResponseBody
    public AjaxResult addDriving(@RequestParam String obj_data){
        JSONObject object=JSONObject.parseObject(obj_data);
        AmDriving amDriving=JSONObject.toJavaObject(object, AmDriving.class);
        amDriving.setDrivingTime(new Date(object.getString("drivingTimes")));
        return toAjax(amDrivingService.insertAmDriving(amDriving));
    }

    @PostMapping("/updateDriving")
    @ResponseBody
    public AjaxResult updateDriving(@RequestParam String obj_data){
        JSONObject object=JSONObject.parseObject(obj_data);
        AmDriving amDriving=JSONObject.toJavaObject(object, AmDriving.class);
        amDriving.setDrivingTime(new Date(object.getString("drivingTimes")));
        return toAjax(amDrivingService.updateAmDriving(amDriving));
    }

    @ResponseBody
    @GetMapping("/getRecentDriving")
    public AjaxResult getDriving(){
        long start = new Date().getTime();
        AmDriving amDriving=new AmDriving();
        List<AmDriving> list = amDrivingService.selectAmDrivingList(amDriving);
        long end = new Date().getTime();
        System.out.println(end-start);
        AjaxResult ajax=AjaxResult.success();
        if(list.size()>10){
            List<AmDriving> temp=new ArrayList<>();
            for(int i=list.size()-10;i<list.size();i++){
                temp.add(list.get(i));
            }
            ajax.put("recentDriving",temp);
        }
        else {
            ajax.put("recentDriving",list);
        }
        return ajax;
    }

}
